#!/bin/ash

[ "$ACTION" == "add" ] || exit 0

PHYNBR=${DEVPATH##*/phy}

[ -n $PHYNBR ] || exit 0

. /lib/functions.sh
. /lib/functions/system.sh

board=$(board_name)

case "$board" in
	ubnt,unifi-ac-hd)
		macaddr_add $(mtd_get_mac_binary EEPROM 0x6) $(($PHYNBR + 1)) > /sys${DEVPATH}/macaddress
		;;
	extreme,ap3935)
		echo "##### PHYNBR=$PHYNBR DEVPATH=$DEVPATH"
		[ "$PHYNBR" = "0" ] && echo $(mtd_get_mac_ascii CFG1 RADIOADDR0) > /sys${DEVPATH}/macaddress
		[ "$PHYNBR" = "1" ] && echo $(mtd_get_mac_ascii CFG1 RADIOADDR1) > /sys${DEVPATH}/macaddress
		;;
esac

OPATH=${DEVPATH##/devices/platform/}
OPATH=${OPATH%%/ieee*}

# 10 radios is enough for anyone!
for i in $(seq 0 9);
  do
  BUS=$(uci get wireless.@wifi-device[$i].path)
  if [ "$BUS " == "$OPATH " ]
      then
      PHYNAME=${DEVPATH##*ieee80211/}
      NPHYNAME=$(uci get wireless.@wifi-device[$i].phyname)
      if [ "$NPHYNAME " != " " ]
          then
          if [ "$PHYNAME " != "$NPHYNAME " ]
              then
              iw $PHYNAME set name $NPHYNAME
          fi
      fi
  fi
done
